﻿using MediatR;
using S.Archives.Infrstructre;
using S.Archives.Read.Api.Dto;
using System.Text;
using static S.Archives.Api.Application.Command.生产提示.ProductionPromptCommand;

namespace S.Archives.Read.Api.Application.CommandHandlers.生产提示实现类
{
    /// <summary>
    /// 中介者实现类—生产提示
    /// </summary>
    public class ProductionPromptCommandHandler : IRequestHandler<GetProductionPromptCommand, List<ProductionPromptDto>>
    {
        private readonly DbHelper db;

        public ProductionPromptCommandHandler(DbHelper db)
        {
            this.db = db;
        }
        /// <summary>
        /// 显示生产提示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<List<ProductionPromptDto>> Handle(GetProductionPromptCommand request, CancellationToken cancellationToken)
        {
            StringBuilder builder = new StringBuilder(@"SELECT b.*,a.PUrl,a.Logo from ProductionPrompt a 
                                                        JOIN (SELECT WanderReproductiveState, COUNT(*) AS Hybridization
                                                        FROM SheepFileModel
                                                        WHERE WanderReproductiveState IS NOT NULL and WanderReproductiveState != 0
                                                        GROUP BY WanderReproductiveState) b
														on a.wanderReproductiveState = b.wanderReproductiveState");
            List<ProductionPromptDto> dtos = (await db.GetListDataBySql<ProductionPromptDto>(builder.ToString()));
            
            return dtos;
        }
    }
}
